package com.base.common.edu.util;

import org.apache.logging.log4j.util.Base64Util;

import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.io.*;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class MailUtil {
	
	// qq ssl
	public static final String senderMailAddr = "728067113@qq.com";
//	public static final String senderNickname = "Jerry.Franklyn";
	public static final String senderMailPassword = "flowdoarmhfmbbce";
	public static final String senderMailServerPort = "465";
	public static final String MAIL_HOST = "smtp.qq.com";
	public static final String Protocol_TYPE = "smtp";
	
	/*
	 * 发送HTML格式邮件无附件
	 */
	public static boolean sendMailHTML( String Subject,
									Map<String, List<String>> recieverMap, 
									String Content
									) throws GeneralSecurityException{
		// 设置邮件会话参数
		Properties props = new Properties();
		// 邮箱的发送服务器地址
		props.setProperty("mail.smtp.host", MAIL_HOST);
		// SSL
		props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
		props.setProperty("mail.smtp.socketFactory.fallback", "false");
		// 邮箱发送服务器端口,这里设置为465端口
		props.setProperty("mail.smtp.port", senderMailServerPort);
		props.setProperty("mail.smtp.socketFactory.port", senderMailServerPort);
		props.put("mail.smtp.auth", "true");
		props.setProperty("mail.smtp.auto", "true");
		props.setProperty("mail.transport.protol", Protocol_TYPE);
		// SSL begin
//		MailSSLSocketFactory sf = new MailSSLSocketFactory();
//		sf.setTrustAllHosts(true);
//		properties.put("mail.smtp.ssl.enable", "true");
//		properties.put("mail.smtp.ssl.socketFactory", sf);
		// SSL end
		Session session=Session.getInstance(props);
		session.setDebug(true);
		MimeMessage message=new MimeMessage(session);
		try {
			if ( recieverMap.get("TO").size() > 0 )
				recieverMap.get("TO").stream().forEach(TO -> {
					try {
						message.addRecipient(MimeMessage.RecipientType.TO,new InternetAddress(TO));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
//					System.out.println("TO " + TO);
				});
			if ( recieverMap.get("CC").size() > 0 )
				recieverMap.get("CC").stream().forEach(CC -> {
					try {
						message.addRecipient(MimeMessage.RecipientType.CC,new InternetAddress(CC));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
//					System.out.println("CC " + CC);
				});
			if ( recieverMap.get("BCC").size() > 0 )	
				recieverMap.get("BCC").stream().forEach(BCC -> {
					try {
						message.addRecipient(MimeMessage.RecipientType.BCC,new InternetAddress(BCC));
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
//					System.out.println("BCC " + BCC);
				});
			message.setFrom(new InternetAddress(senderMailAddr));
			message.setSubject(Subject,"utf-8");
			message.setContent(Content ,"text/html;charset=utf-8");
			message.setSentDate(new Date());
			Transport transport=session.getTransport();
			transport.connect(senderMailAddr, senderMailPassword);
			transport.sendMessage(message,message.getAllRecipients());
			transport.close();
			return true;
		} catch (AddressException e) {
			e.printStackTrace();
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		return false;
	}
	
}
